package com.rainbow.support.job.xxl.feignclient;

import com.alibaba.fastjson.JSONObject;
import com.rainbow.support.common.constant.CommonConstant;
import feign.Response;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * xxlJob客户端
 * 仅支持GULE(JAVA),定时任务模式,
 * BEAN模式请到job-admin配置任务
 * @author 傅为地
 */
@FeignClient(name = "xxlJobApi",url = CommonConstant.EXTRA_XXLJOB_URL_SPEL)
public interface XxlJobApi {

    /**
     * xxl-job登录接口
     *
     * @param params 参数
     * @return 响应信息
     */
    @PostMapping(value = "/login", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    Response login(@RequestBody Map<String, ?> params);

    /**
     * 创建定时任务
     *
     * @param cookie cookie
     * @param params 定时任务参数
     * @return 定时任务ID
     */
    @PostMapping(value = "/jobinfo/add", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    JSONObject add(@RequestHeader("Cookie") String cookie, @RequestBody Map<String, ?> params);

    /**
     * 查看任务详情
     * @param cookie cookie
     * @param id     定时任务ID
     * @return 任务详情
     */
    @PutMapping(value = "/jobinfo/detail")
    JSONObject detail(@RequestHeader("Cookie") String cookie, @RequestParam("id") int id);

    /**
     * 更新定时任务
     *
     * @param cookie cookie
     * @param params 定时任务更新参数
     * @return 执行结果
     */
    @PutMapping(value = "/jobinfo/update", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    JSONObject update(@RequestHeader("Cookie") String cookie, @RequestBody Map<String, ?> params);

    /**
     * 执行定时任务
     * @param cookie cookie
     * @param params 定时任务参数
     * @return 定时任务ID
     */
    @PostMapping(value = "/jobinfo/trigger", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    JSONObject trigger(@RequestHeader("Cookie") String cookie, @RequestBody Map<String, ?> params);

    /**
     * 删除定时任务
     *
     * @param cookie cookie
     * @param id     定时任务更新参数
     * @return 执行结果
     */
    @DeleteMapping(value = "/jobinfo/remove")
    JSONObject remove(@RequestHeader("Cookie") String cookie, @RequestParam("id") int id);

    /**
     * 开启任务
     *
     * @param cookie cookie
     * @param id     定时任务ID
     * @return 执行结果
     */
    @PutMapping(value = "/jobinfo/start")
    JSONObject start(@RequestHeader("Cookie") String cookie, @RequestParam("id") int id);

    /**
     * 结束任务
     *
     * @param cookie cookie
     * @param id     定时任务ID
     * @return 执行结果
     */
    @PutMapping(value = "/jobinfo/stop")
    JSONObject stop(@RequestHeader("Cookie") String cookie, @RequestParam("id") int id);

    /**
     * 快捷创建并启动gule任务
     * @param cookie cookie
     * @param params 定时任务参数
     * @return gule任务详情
     */
    @PostMapping(value = "/jobinfo/quickstart", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    JSONObject quickstart(@RequestHeader("Cookie") String cookie, @RequestBody Map<String, ?> params);

    /**
     * 查询执行器
     * @param cookie 浏览器cookie
     * @param params 请求参数
     * @return 执行器列表
     */
    @PostMapping(value = "/jobgroup/pageList", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    JSONObject pageList(@RequestHeader("Cookie") String cookie, @RequestBody Map<String, ?> params);


}
